# -*- coding: utf-8 -*-
# @Time     : 2021/12/1 14:21
# @Author   : wljess
# @File     : 线程和进程执行效率对比.py
# @Describe : 
# @Software : PyCharm

import time
from multiprocessing import Process
from threading import Thread


def efficiency(func):
    def inner(*args, **kwargs):
        flag = args[0]
        start_time = time.perf_counter()
        func(*args, **kwargs)
        end_time = time.perf_counter()
        if isinstance(flag, Thread):
            print(f"线程运行时间：{end_time - start_time}s")
        else:
            print(f"进程运行时间：{end_time - start_time}s")
    return inner


def task(a, b):

    c = a + b


@efficiency
def efficiency_test(multitask, num):
    lst_obj = []
    for i in range(num):
        obj = multitask(target=task, args=(i, i*2))

        obj.start()
        lst_obj.append(obj)

    for obj in lst_obj:
        obj.join()


if __name__ == '__main__':
    efficiency_test(Thread, 200)
    efficiency_test(Process, 200)

    print("主线程运行结束...")


